using System;
using System.Collections;
using System.Management;

namespace Connect
{
	/// <summary>
	/// A Helper class which provides convenient methods to set/get network
	/// configuration
	/// </summary>
    public class WMIHelper
	{
		#region Public Static

		/// <summary>
		/// Enable DHCP on the NIC
		/// </summary>
		/// <param name="nicName">Name of the NIC</param>
		public static void SetDHCP( string nicName )
		{
			ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
			ManagementObjectCollection moc = mc.GetInstances();

			foreach(ManagementObject mo in moc)
			{
				// Make sure this is a IP enabled device. Not something like memory card or VM Ware
				if( (bool)mo["IPEnabled"] )
				{
					if( mo["Caption"].Equals( nicName ) )
					{
						ManagementBaseObject newDNS = mo.GetMethodParameters( "SetDNSServerSearchOrder" );
						newDNS[ "DNSServerSearchOrder" ] = null;
						ManagementBaseObject enableDHCP = mo.InvokeMethod( "EnableDHCP", null, null);
						ManagementBaseObject setDNS = mo.InvokeMethod( "SetDNSServerSearchOrder", newDNS, null);
					}
				}
			}
		}
		
		/// <summary>
		/// Set IP for the specified network card name
		/// </summary>
		/// <param name="nicName">Caption of the network card</param>
		/// <param name="IpAddresses">Comma delimited string containing one or more IP</param>
		/// <param name="SubnetMask">Subnet mask</param>
		/// <param name="Gateway">Gateway IP</param>
		/// <param name="DnsSearchOrder">Comma delimited DNS IP</param>
		public static void SetIP( string nicName, string IpAddresses, string SubnetMask, string Gateway, string DnsSearchOrder)
		{
			ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
			ManagementObjectCollection moc = mc.GetInstances();

			foreach(ManagementObject mo in moc)
			{
				// Make sure this is a IP enabled device. Not something like memory card or VM Ware
				if( (bool)mo["IPEnabled"] )
				{
					if( mo["Caption"].Equals( nicName ) )
					{

						ManagementBaseObject newIP = mo.GetMethodParameters( "EnableStatic" );
						ManagementBaseObject newGate = mo.GetMethodParameters( "SetGateways" );
						ManagementBaseObject newDNS = mo.GetMethodParameters( "SetDNSServerSearchOrder" );
								
						newGate[ "DefaultIPGateway" ] = new string[] { Gateway };
						newGate[ "GatewayCostMetric" ] = new int[] { 1 };

						newIP[ "IPAddress" ] = IpAddresses.Split( ',' );
						newIP[ "SubnetMask" ] = new string[] { SubnetMask };

						newDNS[ "DNSServerSearchOrder" ] = DnsSearchOrder.Split(',');

						ManagementBaseObject setIP = mo.InvokeMethod( "EnableStatic", newIP, null);
						ManagementBaseObject setGateways = mo.InvokeMethod( "SetGateways", newGate, null);
						ManagementBaseObject setDNS = mo.InvokeMethod( "SetDNSServerSearchOrder", newDNS, null);

						break;
					}
				}
			}
		}

		/// <summary>
		/// Returns the network card configuration of the specified NIC
		/// </summary>
		/// <param name="nicName">Name of the NIC</param>
		/// <param name="ipAdresses">Array of IP</param>
		/// <param name="subnets">Array of subnet masks</param>
		/// <param name="gateways">Array of gateways</param>
		/// <param name="dnses">Array of DNS IP</param>
		public static void GetIP( string nicName, out string [] ipAdresses, out string [] subnets, out string [] gateways, out string [] dnses )
		{
			ipAdresses = null;
			subnets = null;
			gateways = null;
			dnses = null;

			ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
			ManagementObjectCollection moc = mc.GetInstances();

			foreach(ManagementObject mo in moc)
			{
				// Make sure this is a IP enabled device. Not something like memory card or VM Ware
				if( (bool)mo["ipEnabled"] )
				{
					if( mo["Caption"].Equals( nicName ) )
					{
						ipAdresses = (string[]) mo["IPAddress"];
						subnets = (string[]) mo["IPSubnet"];
						gateways = (string[]) mo["DefaultIPGateway"];
						dnses = (string[]) mo["DNSServerSearchOrder"];

						break;
					}
				}
			}
		}

		/// <summary>
		/// Returns the list of Network Interfaces installed
		/// </summary>
		/// <returns>Array list of string</returns>
		public static ArrayList GetNICNames()
		{
			ArrayList nicNames = new ArrayList();

			ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
			ManagementObjectCollection moc = mc.GetInstances();

			foreach(ManagementObject mo in moc)
			{
				if((bool)mo["ipEnabled"])
				{
					nicNames.Add( mo["Caption"] );
				}
			}

			return nicNames;
		}

		#endregion
	}
}
